package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.activities;
import com.cdut.hwadeefsc.service.activitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
    
@RestController
@RequestMapping("/api/activities")
public class activityController {  // 修改类名首字母大写

    @Autowired
    private activitiesService activitiesService;

    // 发布学生活动（老师权限）
    @PostMapping("/publish")
    public ResponseEntity<?> publishActivity(@RequestBody activities activity) {
        try {
            boolean success = activitiesService.addActivity(activity);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 为指定班级发布活动（老师权限）
    @PostMapping("/class/{classId}/publish")
    public ResponseEntity<?> publishClassActivity(@PathVariable Long classId, @RequestBody activities activity) {  // Integer → Long
        try {
            activity.setClassId(classId);
            boolean success = activitiesService.addActivity(activity);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 查看班级活动
    @GetMapping("/class/{classId}")
    public ResponseEntity<List<activities>> getClassActivities(@PathVariable Long classId) {  // Integer → Long
        try {
            List<activities> activities = activitiesService.getActivitiesByClassId(classId);
            return ResponseEntity.ok(activities);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 查看所有可见的学生活动（家长权限）
    @GetMapping("/all")
    public ResponseEntity<List<activities>> getAllActivities() {
        try {
            List<activities> activities = activitiesService.getAllActivities();
            return ResponseEntity.ok(activities);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 查看学生个人活动（通过学生ID）
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<activities>> getStudentActivities(@PathVariable Long studentId) {  // Integer → Long
        try {
            List<activities> activities = activitiesService.getActivitiesByStudentId(studentId);
            return ResponseEntity.ok(activities);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据ID查看活动详情
    @GetMapping("/{activityId}")
    public ResponseEntity<activities> getActivityById(@PathVariable Long activityId) {  // Integer → Long
        try {
            activities activity = activitiesService.getActivityById(activityId);
            if (activity != null) {
                return ResponseEntity.ok(activity);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 更新活动信息
    @PutMapping("/{activityId}")
    public ResponseEntity<?> updateActivity(@PathVariable Long activityId, @RequestBody activities activity) {  // Integer → Long
        try {
            activity.setId(activityId);
            boolean success = activitiesService.updateActivity(activity);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 删除活动
    @DeleteMapping("/{activityId}")
    public ResponseEntity<?> deleteActivity(@PathVariable Long activityId) {  // Integer → Long
        try {
            boolean success = activitiesService.deleteActivity(activityId);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据状态筛选活动
    @GetMapping("/status/{status}")
    public ResponseEntity<List<activities>> getActivitiesByStatus(@PathVariable String status) {
        try {
            List<activities> activities = activitiesService.getActivitiesByStatus(status);
            return ResponseEntity.ok(activities);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
